<template>
  <div class="dashboard-container">
    <h1>房间类型管理</h1>

    <div>
      <el-button type="primary" @click="addButton" style="width:100px;margin-left:40px">新增</el-button>

      <el-input placeholder="房间名称" v-model="roomTypeName" clearable style="width:200px;margin-left:40px"/>
      <el-input placeholder="房间价格" v-model="roomPrice" clearable style="width:200px;margin-left:40px"/>



      <el-button type="primary" @click="selectAll" style="width:100px;margin-left:40px">查询</el-button>
      <el-button @click="checkUser" style="width:100px;margin-left:40px">重置</el-button>


      <el-table :data="roomTypeList" height="659px" border style="width: 100%;margin-top: 20px">
        <el-table-column align="center" fixed prop="roomTypeId" label="房间类型id" sortable/>
        <el-table-column align="center" fixed prop="roomTypeName" label="房间类型名称" sortable/>
        <el-table-column align="center" fixed prop="roomPrice" label="房间价格" sortable/>
        <el-table-column align="center" fixed prop="remark" label="备注" sortable/>
        <el-table-column align="center" fixed label="操作">

          <template v-slot="scope">
            <el-button type="text" @click="updateBound(scope.row)" >修改</el-button>
            <el-button type="text" @click="deleteBound(scope.row)" v-if="scope.row.roleId != 1">删除</el-button>
            <el-button type="text" disabled v-else>删除</el-button>
          </template>
        </el-table-column>


      </el-table>
      <el-dialog title="是否删除" width="40%" :visible.sync="deleteButtonController" >
        <!--        <el-form label-width="20%" :rules="roles" :model="addList" ref="ruleForm"></el-form>-->
        <span slot="footer" class="dialog-footer">
             <el-button type="primary" @click="deleteUser">确定</el-button>
              <el-button @click="closeUser = false">取消</el-button>
          </span>
      </el-dialog>


      <el-dialog title="新增" width="40%" :visible.sync="addButtonController" @close="closeUser('ruleForm')">
        <!--        <el-form label-width="20%" :rules="roles" :model="addList" ref="ruleForm"></el-form>-->
        <el-form label-width="20%" :rules="roles" :model="addList" ref="ruleForm">
          <el-form-item label="房间类型名称" prop="roomTypeName">
            <el-input v-model="addList.roomTypeName" maxlength="20" show-word-limit style="width: 80%" clearable
                      placeholder="房间类型名称"/>
          </el-form-item>
          <el-form-item label="房间价格" prop="roomPrice">
            <el-input v-model="addList.roomPrice" maxlength="6" show-word-limit style="width: 80%" clearable
                      placeholder="房间价格"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="addList.remark" maxlength="10" show-word-limit style="width: 40%" clearable
                      placeholder="请输入备注"/>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUser('ruleForm')">确定</el-button>
        <el-button @click="closeUser('ruleForm')">取消</el-button>
      </span>
      </el-dialog>

      <el-dialog title="修改" width="40%" :visible.sync="updateButtonController" @close="closeUser('ruleForm')">
        <!--        <el-form label-width="20%" :rules="roles" :model="addList" ref="ruleForm"></el-form>-->
        <el-form label-width="20%" :rules="roles" :model="updateList" ref="ruleForm">
          <el-form-item label="房间类型名称" prop="roomTypeName">
            <el-input v-model="updateList.roomTypeName" maxlength="20" show-word-limit style="width: 80%" clearable
                      placeholder="房间类型名称"/>
          </el-form-item>
          <el-form-item label="房间价格" prop="roomPrice">
            <el-input v-model="updateList.roomPrice" maxlength="6"  show-word-limit style="width: 80%" clearable
                      placeholder="房间价格"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="updateList.remark" maxlength="10" show-word-limit style="width: 40%" clearable
                      placeholder="请输入备注"/>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="updateUser('ruleForm')">确定</el-button>
        <el-button @click="closeUser('ruleForm')">取消</el-button>
      </span>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import {mapGetters} from 'vuex'
import {addUser, deleteByUserId, selecctAllUser} from '@/api/user'
import {deletes, inserts, selectRoomType, update} from "@/api/RoomType";

export default {
  name: 'roomType',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },

  data() {
    return {
      roomTypeName: '',
      roomPrice: '',
      roomTypeList: [],
      // 添加新增
      addButtonController: false,
      addList: {
        roomTypeName: '',
        roomPrice: '',
        remark: ''
      },
      // 修改
      updateButtonController: false,
      updateList: {
        roomTypeName: '',
        roomPrice: '',
        roomTypeTableId: '',
        roomTypeId: '',
        remark: ''
      },
      // 删除
      deleteButtonController: false,
      roomTypeId: '',
      roomTypeTableId: '',
      roles: {
        roomTypeName: [
          {required: true, trigger: 'blur', message: '不能为空'},
          {min: 1, max: 20, message: '格式错误', trigger: 'blur'}],
        roomPrice: [
          {required: true, trigger: 'blur', message: '不能为空'},
          {min: 1, max: 6, message: '格式错误', trigger: 'blur'}]
      }
    }
  },
  mounted() {
    this.selectAll()
  },
  methods: {
    selectAll() {
      // if (this.userId1==null || this.userName1==null || this.roleName1){
      //   this.disabled=no
      // }else
      console.log('roomTypeId:' + this.roomTypeId)
      console.log('roomTypeName:' + this.roomTypeName)
      console.log('roomPrice:' + this.roomPrice)
      selectRoomType({
        roomTypeName: this.roomTypeName,
        roomPrice: this.roomPrice
      }).then(
        response => {
          console.log(response.data)
          this.roomTypeList = response.data
        }
      )
    },
    checkUser() {
      this.roomTypeName = ''
      this.roomPrice = ''
      this.selectAll()
    },
    addButton() {
      this.addButtonController = true
      this.addList = {
        roomTypeName: '',
        roomPrice: '',
        remark: ''
      }
    },
    addUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          inserts({
            roomPrice: this.addList.roomPrice,
            roomTypeName: this.addList.roomTypeName,
            remark: this.addList.remark
          }).then(
            response => {
              console.log(response.data)
              this.addButtonController = false
              this.selectAll()
            }
          )
        } else {
          return false
        }
      })
    },
    updateBound(row) {
      this.updateList = {
        roomPrice: row.roomPrice,
        roomTypeName: row.roomTypeName,
        roomTypeTableId: row.roomTypeTableId,
        roomTypeId: row.roomTypeId,
        remark: row.remark
      }
      this.updateButtonController = true
    },
    updateUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          update({
            roomPrice: this.updateList.roomPrice,
            roomTypeName: this.updateList.roomTypeName,
            roomTypeTableId: this.updateList.roomTypeTableId,
            roomTypeId: this.updateList.roomTypeId,
            remark: this.updateList.remark
          }).then(
            response => {
              this.updateButtonController = false
              this.selectAll()
            }
          )
        } else {
          return false
        }
      })
    },

    deleteBound(row) {
      this.deleteButtonController = true
      this.roomTypeId = row.roomTypeId
      this.roomTypeTableId = row.roomTypeTableId
    },
    deleteUser() {
      deletes({
        roomTypeId: this.roomTypeId,
        roomTypeTableId: this.roomTypeTableId
      }).then(
        response => {
          this.deleteButtonController = false
          this.selectAll()
        }
      )
    },
    closeUser(formName) {
      this.$refs[formName].resetFields();
      this.addButtonController = false
      this.updateButtonController = false
      this.deleteButtonController = false
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
